package com.jstyle.jclife.activity;

import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.model.NetResultData;

import com.jstyle.jclife.utils.NetWorkConast;
import com.jstyle.jclife.utils.SHA1;
import com.jstyle.jclife.utils.ScreenUtils;
import com.jstyle.jclife.view.EditTextWithDelete;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class UpdatePwdActivity extends BaseActivity {

    @BindView(R.id.tv_register_title)
    TextView tvRegisterTitle;
    @BindView(R.id.tv_register_tips)
    TextView tvRegisterTips;
    @BindView(R.id.edit_currentPwd)
    EditTextWithDelete editCurrentPwd;
    @BindView(R.id.edit_newPwd)
    EditTextWithDelete editNewPwd;
    @BindView(R.id.edit_confirmPwd)
    EditTextWithDelete editConfirmPwd;
    @BindView(R.id.bt_updatepwd_back)
    Button btUpdatepwdBack;
    @BindView(R.id.bt_updatepwd_next)
    Button btUpdatepwdNext;
    @BindView(R.id.tv_register_tologin)
    TextView tvRegisterTologin;
    private Disposable resetDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pwd);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        tvRegisterTologin.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);
    }

    @OnClick({R.id.bt_updatepwd_back, R.id.bt_updatepwd_next, R.id.tv_register_tologin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_updatepwd_back:
                finish();
                break;
            case R.id.bt_updatepwd_next:
                updatePwd();
                break;
            case R.id.tv_register_tologin:
                startActivity(ForgotPwdActivity.class);
                break;
        }
    }

    private void updatePwd() {
        String currentPwd=editCurrentPwd.getText().toString();
        String newPwd=editNewPwd.getText().toString();
        String confirmPwd=editConfirmPwd.getText().toString();
        if (TextUtils.isEmpty(confirmPwd) || TextUtils.isEmpty(newPwd) || TextUtils.isEmpty(currentPwd)) {
            showToast(getString(R.string.fill_all_info));
            return;
        }
        if (newPwd.length() < 6) {
            showToast(getString(R.string.psd_too_short));
            return;
        }
        if(!newPwd.equals(confirmPwd)){
            showToast(getString(R.string.sure_psd_same));
            return;
        }
        Map<String, Object> map = new HashMap<>();
        String uid=NetWorkUtil.getUserId();
        map.put(NetWorkConast.KEY_userpwd, SHA1.md5Encoder(newPwd));
        map.put(NetWorkConast.KEY_OldPwd, SHA1.md5Encoder(currentPwd));
        map.put(NetWorkConast.KEY_uid, uid);
        NetWorkUtil.addSHA1(map);
        NetWorkUtil.getInstance().getJstyleApi().updatePwd(map).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                resetDisposable =disposable;
                showProgressDialog(getString(R.string.wait));
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NetResultData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(NetResultData netResultData) {
                disMissProgressDialog();
                int msgCode = netResultData.getMsgCode();
                String msg = NetWorkConast.getResult(msgCode, UpdatePwdActivity.this);
                if (msgCode == NetResultData.Success_Code) {
                    ScreenUtils.showSetSuccessFul(editConfirmPwd,getString(R.string.Update_Password_Successful));
                    //   msg = getString(R.string.login_complete);
                    // sendEmailSuccessful();
                }else{
                    showToast(msg);
                }
                //
            }

            @Override
            public void onError(Throwable e) {
                disMissProgressDialog();
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unSubscribe(resetDisposable);
    }
}
